﻿using Knc.Core.ViewModel;
using Knc.Enum;
using Knc.Model;
using Slc.Core.DataProvider;
using Slc.Core.DataProvider.Adapter;
using Slc.Core.Entity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Knc.Core
{
    public class CateService : BaseService
    {
        public List<Category> GetAllCate()
        {
            Category cate = new Category();
            OQL oql = OQL.From(cate)
                .Select().Where(th => th.Comparer(cate.CateState, OQLCompare.CompareType.Equal, CateState.Category.ToString()))
                .END;
            List<Category> li = new List<Category>();
            using (AdoHelper DB = SlcDB.GetDBHelperByConn(ConnectionString))
            {
                li = EntityQuery.QueryList<Category>(oql, DB);
            }
            return li;
        }

        public Category GetCateById(string CateId)
        {
            Category cate = new Category();
            OQL oql = OQL.From(cate).Select()
                .Where(th => th.Comparer(cate.CateID, OQLCompare.CompareType.Equal, CateId))
                .END;
            using (AdoHelper DB = SlcDB.GetDBHelperByConn(ConnectionString))
            {
                cate = EntityQuery.QueryObject<Category>(oql, DB);
            }
            return cate;
        }

        public List<Category> GetAllFastLable(string CateId)
        {
            Category cate = new Category();
            OQL oql = OQL.From(cate)
                .Select().Where(th =>
                    th.Comparer(cate.CateState, OQLCompare.CompareType.Equal, CateState.FastLable.ToString()) &
                    th.Comparer(cate.PreCateId, OQLCompare.CompareType.Equal, CateId)
                )
                .END;
            List<Category> li = new List<Category>();
            using (AdoHelper DB = SlcDB.GetDBHelperByConn(ConnectionString))
            {
                li = EntityQuery.QueryList<Category>(oql, DB);
            }
            return li;
        }

        public CateVModel GetCate(string CateId)
        {
            Category cate = GetCateById(CateId);
            if (cate == null) return new CateVModel();
            List<Category> li = GetAllFastLable(CateId);
            List<CateVModel> liCateVChild = new List<CateVModel>();
            CateVModel vm = new CateVModel();
            vm.CateId = cate.CateID;
            vm.CateCode = cate.CateCode;
            vm.CateName = cate.CateName;
            vm.Created = cate.Created;
            vm.CreateTime = cate.CreateTime;
            vm.Remark = cate.Remark;
            vm.CateDesc = cate.CateDesc;
            vm.CateState = cate.CateState;
            vm.ImgUrl = cate.ImgUrl;
            foreach (var item in li)
            {
                CateVModel vmTemp = new CateVModel();
                vmTemp.CateId = item.CateID;
                vmTemp.CateCode = item.CateCode;
                vmTemp.CateName = item.CateName;
                vmTemp.Created = item.Created;
                vmTemp.CreateTime = item.CreateTime;
                vmTemp.Remark = item.Remark;
                vmTemp.CateDesc = item.CateDesc;
                vmTemp.CateState = item.CateState;
                liCateVChild.Add(vmTemp);
            }
            vm.CateChild = liCateVChild;
            return vm;
        }
        public List<CateVModel> GetCateList()
        {
            List<Category> li = GetAllCate();
            List<CateVModel> liCateV = new List<CateVModel>();
            List<CateVModel> liCateVChild;
            foreach (var item in li.Where(th => th.PreCateId == "0").ToList())
            {
                CateVModel vm = new CateVModel();
                vm.CateId = item.CateID;
                vm.CateCode = item.CateCode;
                vm.CateName = item.CateName;
                vm.Created = item.Created;
                vm.CreateTime = item.CreateTime;
                vm.Remark = item.Remark;
                vm.CateDesc = item.CateDesc;
                vm.CateState = item.CateState;
                vm.ImgUrl = item.ImgUrl;
                liCateVChild = new List<CateVModel>();
                foreach (var itemChild in li.Where(th => th.PreCateId == item.CateID).ToList())
                {
                    CateVModel Childvm = new CateVModel();
                    Childvm.CateId = itemChild.CateID;
                    Childvm.CateCode = itemChild.CateCode;
                    Childvm.CateName = itemChild.CateName;
                    Childvm.Created = itemChild.Created;
                    Childvm.CreateTime = itemChild.CreateTime;
                    Childvm.Remark = itemChild.Remark;
                    Childvm.CateDesc = itemChild.CateDesc;
                    Childvm.CateState = itemChild.CateState;
                    Childvm.ImgUrl = itemChild.ImgUrl;
                    liCateVChild.Add(Childvm);
                }
                vm.CateChild = liCateVChild;
                liCateV.Add(vm);
            }
            return liCateV;
        }
    }
}
